﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _40_C_中两种常用实现单例模式的方式
{

    internal class Singleton3<T> where T:class //泛型单例 引用类型约束
    {
        private static readonly object _lock = new object();

        private Singleton3()
        {

        }

        private static T Object_;

        public static T Instance
        {
            get
            {

                if (Object_ == null)
                {
                    lock (_lock)//防止多线程
                    {
                        if (Object_ == null)
                        {
                            Object_ = System.Activator.CreateInstance<T>();
                        }
                        return Object_;
                    }
                }
                return Object_;
            }
        }
    }
}
